<template>
  <!-- 页面主体结构 -->
  <view class="page">
    <!-- 顶部导航栏组件 -->
    <u-navbar :fixed="true" bgColor="#FFFFFF00" placeholder @leftClick="back">
      <view class="navTitle" slot="left" :style="'color:' + titleColor">
        <u-icon name="arrow-left" color="#000"></u-icon>
      </view>
    </u-navbar>
    <!-- LOGO展示区域 -->
    <view class="logo">
      <image
        class="logoImg"
        src="../../static/tarBar/center.png"
        mode="aspectFit"
      ></image>
    </view>
    <!-- 使用三元判断是否触发新用户获取手机号 -->
    <!-- 快速登录按钮：根据是否为新用户决定是否获取手机号 -->
    <button
      class="btn"
      @getphonenumber="getphonenumber"
      open-type="getPhoneNumber"
    >
      快速登录
    </button>
    <!-- 用户协议勾选区域 -->
    <view class="userNotice">
      <u-checkbox-group
        v-model="checkbox"
        placement="circle"
        shape="circle"
        size="40"
        iconSize="40"
        @change="checkboxChange"
      >
        <u-checkbox name="" labelSize="32" label="我已阅读并同意"></u-checkbox>
      </u-checkbox-group>
      <view>
        <text style="color: #3c8dff" @tap="show = true">《用户隐私政策》</text>
      </view>
      <u-popup
        :show="show"
        :safeAreaInsetBottom="true"
        :round="10"
        closeIconPos="top-right"
        mode="bottom"
        @close="close"
        @open="open"
        :customStyle="{ ' width': '100%' }"
      >
        <view>
          <view
            style="
              padding: 40rpx 44rpx;
              display: flex;
              flex-direction: column;
              align-items: center;
              height: 503rpx;
              overflow: auto;
            "
          >
            <p style="font-weight: Bold; font-size: 28rpx">
              请阅读并同意用户协议
            </p>
            <rich-text :nodes="userNotice"></rich-text>
          </view>
        </view>
      </u-popup>
    </view>
  </view>
</template>
<script>
// 导入相关API和配置
import { getPhone, getAgreement, login } from "@/api/user";
import { getUserInfo } from "@/api/v2";
import config from "@/utils/config";
export default {
  data() {
    return {
      themeColors: uni.getStorageSync("color"), // 主题颜色
      checkbox: false, // 协议勾选状态
      show: false, // 协议弹窗显示状态
      lock: false, // 登录按钮锁定状态
      isNew: false, // 是否为新用户
      userNotice: "", // 用户协议内容
      localUrl: "", // 登录后跳转的目标页面
      token: "", // 用户令牌
      defaultTime: "", // 默认时间戳
      loginRes: {},
    };
  },
  onShow() {
    //获取协议请求
    getAgreement({
      obj_id: config.obj_id,
      type: 1,
    }).then((res) => {
      this.userNotice = res.data.content;
    });
  },


  onLoad(options) {
    this.localUrl = options.data;
    this.defaultTime = Date.now()
    uni.login({
        provider: "weixin",
        scopes: "auth_user",
        success: (loginRes) => {
          console.log(loginRes)
          this.loginRes = loginRes;
        },
    });
    console.log(this.loginRes)
  },
  methods: {
    // 处理用户授权手机号的流程
    async getphonenumber(e) {
      if(!this.loginRes.code){
        uni.showToast({
          title: "操作太快，请稍后操作",
          icon: "none",
        });
        uni.login({
          provider: "weixin",
          scopes: "auth_user",
          success: (loginRes) => {
            console.log(loginRes)
            this.loginRes = loginRes;
          },
        });
        return
      }
      const detail = e.detail;
      let that =this
      await login({
        iv: detail.iv,
        encrypted_data: detail.encryptedData,
        js_code: that.loginRes.code,
        code: config.project,
        type: 0,
      }).then((res) => {
        if (res.code == "0") {
          uni.showToast({
            title: res.msg,
            icon: "none",
          });
        } else {
          uni.hideLoading();
          uni.setStorageSync("token", res.data.token);
          uni.setStorageSync("phone", res.data.phone);
          uni.setStorageSync("openid", res.data.openid);
          uni.setStorageSync("defaultTime", this.defaultTime + 259200000);
          if (that.localUrl.substring(0, 6) == "pages/") {
            if (that.localUrl == "pages/mine/index") {
              uni.switchTab({ url: "/pages/index/index" });
            } else {
              uni.switchTab({ url: "/" + that.localUrl });
            }
          } else {
            uni.redirectTo({ url: "/" + that.localUrl });
          }
        }
      });
    },
    // 用户协议勾选状态变更处理
    checkboxChange(e) {
      if (e.length !== 0) {
        this.checkbox = e[0];
      } else {
        this.checkbox = false;
      }
    },
    // 返回按钮处理
    // 清除存储的用户信息并返回指定页面
    back() {
      uni.removeStorageSync("uniond");
      uni.removeStorageSync("openid");
      uni.removeStorageSync("token");
      if (this.localUrl.substring(0, 6) == "pages/") {
        if (
          this.localUrl == "pages/mine/index" ||
          this.localUrl == "pages/center/index"
        ) {
          uni.switchTab({ url: "/pages/index/index" });
        } else {
          uni.switchTab({ url: "/" + this.localUrl });
        }
      } else {
        uni.redirectTo({ url: "/" + this.localUrl });
      }
    },
    close() {
      this.show = false;
    },
  },
};
</script>
<style lang="scss" scoped>
.page {
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding-bottom: 0;
  padding-bottom: constant(safe-area-inset-bottom);
  padding-bottom: env(safe-area-inset-bottom);
}

.logo {
  width: 256rpx;
  height: 256rpx;
  position: fixed;
  top: 177px;
}

.logoImg {
  width: 256rpx;
  height: 256rpx;
}

.btn {
  background: #004098;
  border-radius: 100px;
  position: fixed;
  padding: 0 180rpx;
  bottom: 300px;
  margin: 0 auto;
  color: #fff;
  button::after {
    border: none;
  }
}

.userNotice {
  position: fixed;
  bottom: 250px;
  display: flex;
  align-items: center;
}
</style>